#!/bin/bash

##############################################################                  
#  更新分销商的等级
##############################################################
      
##  更新2等级的分销商
##############################################################
    connkpi -e "UPDATE base_user SET currentType=2 WHERE id IN (
					SELECT userId 
					FROM dwd_distribution_income
					LEFT JOIN (
							SELECT 
							MAX(CASE WHEN configname='distributor_one_level_limit' THEN configvalue END) onelimit,
							MAX(CASE WHEN configname='distributor_two_level_limit' THEN configvalue END) twolimit,
							MAX(CASE WHEN configname='distributor_three_level_limit' THEN configvalue END) threelimit
							FROM base_system_settings 
							WHERE configname IN ('distributor_one_level_limit','distributor_three_level_limit','distributor_two_level_limit') 
							) config ON 1=1
					WHERE current_day>twolimit AND current_day< threelimit);
                   "
    #判断前句命令执行是否成功 0为成功,非0为失败.
    if [ $? != 0 ]; then
        exit 1
    fi
echo "2级分销商等级更新完成"
      
##  更新3等级的分销商
##############################################################
    connkpi -e "UPDATE base_user SET currentType=3 WHERE id IN (
						SELECT userId 
						FROM dwd_distribution_income
						LEFT JOIN (
								SELECT 
								MAX(CASE WHEN configname='distributor_one_level_limit' THEN configvalue END) onelimit,
								MAX(CASE WHEN configname='distributor_two_level_limit' THEN configvalue END) twolimit,
								MAX(CASE WHEN configname='distributor_three_level_limit' THEN configvalue END) threelimit
								FROM base_system_settings 
								WHERE configname IN ('distributor_one_level_limit','distributor_three_level_limit','distributor_two_level_limit') 
								) config ON 1=1
						WHERE current_day>threelimit);
                   "
    #判断前句命令执行是否成功 0为成功,非0为失败.
    if [ $? != 0 ]; then
        exit 1
    fi
echo "3级分销商等级更新完成"

##  更新1等级的分销商
##############################################################
    connkpi -e "UPDATE base_user SET currentType=1 WHERE id IN (
						SELECT userId 
						FROM dwd_distribution_income
						LEFT JOIN (
								SELECT 
								MAX(CASE WHEN configname='distributor_one_level_limit' THEN configvalue END) onelimit,
								MAX(CASE WHEN configname='distributor_two_level_limit' THEN configvalue END) twolimit,
								MAX(CASE WHEN configname='distributor_three_level_limit' THEN configvalue END) threelimit
								FROM base_system_settings 
								WHERE configname IN ('distributor_one_level_limit','distributor_three_level_limit','distributor_two_level_limit') 
								) config ON 1=1
						WHERE current_day>onelimit and current_day<twolimit);
                   "
    #判断前句命令执行是否成功 0为成功,非0为失败.
    if [ $? != 0 ]; then
        exit 1
    fi
echo "3级分销商等级更新完成"
echo "数据更新成功。"
exit 0